package edu.nju.desserthouse.servlets;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.sql.Date;
import nju.edu.desserthouse.model.Vip;
import edu.nju.desserthouse.action.business.VipInfoBean;
import edu.nju.desserthouse.dao.VipDao;

/**
 * Servlet implementation class AutoDone
 */
@WebServlet("/news.manager")
public class AutoDone extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
    
	@EJB
	VipDao vipDao;
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AutoDone() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		execute (request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		execute (request, response);
	}

	@SuppressWarnings("deprecation")
	public void execute(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
		Date now = new Date(new java.util.Date().getTime());
		int pauseYear = now.getYear() - 1;
		int stopYear = now.getYear() -2;
		
		Date pauseDate = new Date(new java.util.Date().getTime());
		pauseDate.setYear(pauseYear);
		Date stopDate = new Date(new java.util.Date().getTime());
		stopDate.setYear(stopYear);
		
		int pauseCount = 0;
		VipInfoBean vipInfo = new VipInfoBean();
		vipInfo.setVipList(vipDao.getVipList(pauseDate));
		for(int i = 0; i < vipInfo.getListSize(); i++){
			Vip vip = vipInfo.getVip(i);
			vip.setState(1);
			vipDao.updateByVipId(vip);
			pauseCount ++;
		}
		
		int stopCount = 0;
		vipInfo.setVipList(vipDao.getVipList(stopDate));
		for(int i = 0; i < vipInfo.getListSize(); i++){
			Vip vip = vipInfo.getVip(i);
			vip.setState(2);
			vipDao.updateByVipId(vip);
			stopCount ++;
		}
		req.getSession().setAttribute("pauseCount", pauseCount);
		req.getSession().setAttribute("stopCount", stopCount);
		resp.sendRedirect("/desserthouseWEB/manager/vipnews.jsp");
	}
}
